{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "accounting_factors = [\n",
    "    \"Size\",\n",
    "    \"Growth\",\n",
    "    \"Lev\",\n",
    "    \"FCF\",\n",
    "    \"PPE\",\n",
    "    \"ROA\",\n",
    "    \"ROE\",\n",
    "    \"TobinQ\",\n",
    "    \"Top1\",\n",
    "    \"Top10\",\n",
    "    \"Inventory\",\n",
    "    \"InvTurnover\",\n",
    "    \"lnBoard\",\n",
    "    \"pctIndepen\",\n",
    "    \"Age\",\n",
    "    \"Big4\",\n",
    "    \"DUAL\",\n",
    "    \"SOE\",\n",
    "    \"Loss\",\n",
    "    \"DvdPayout\",\n",
    "    \"Quick\",\n",
    "    \"Current\",\n",
    "    \"ICRebitda\",\n",
    "]\n",
    "\n",
    "market_factors = [\n",
    "    \"ret\",\n",
    "    \"annualret\",\n",
    "    \"volatility\",\n",
    "    \"turnover\",\n",
    "    \"PE\",\n",
    "    \"institute\",\n",
    "    \"coverage\",\n",
    "    \"brokers\",\n",
    "    \"MV\",\n",
    "]\n",
    "\n",
    "academic_factors = [\n",
    "    \"avLoss\",\n",
    "    \"NCSKEW\",\n",
    "    \"DUVOL\",\n",
    "    \"KZologit\",\n",
    "    \"SA\",\n",
    "    \"DA\",\n",
    "    \"REM\",\n",
    "    \"PLDdummy\",\n",
    "    \"ETR1\",\n",
    "    \"BTD\",\n",
    "    \"RiskT\",\n",
    "    \"avgLawTax\",\n",
    "    \"RPTratio\",\n",
    "]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Counter({0: 14190, 1: 547})\n",
      "Counter({0: 14190, 1: 14190})\n",
      "0.9259894200626959\n",
      "Counter({0: 14481, 1: 256})\n",
      "Counter({0: 14481, 1: 14481})\n",
      "0.844285615838231\n",
      "Counter({0: 14165, 1: 572})\n",
      "Counter({0: 14165, 1: 14165})\n",
      "0.8449742486120908\n",
      "Counter({0: 14118, 1: 619})\n",
      "Counter({0: 14118, 1: 14118})\n",
      "0.9190833672030029\n",
      "Counter({0: 12749, 1: 1988})\n",
      "Counter({0: 12749, 1: 12749})\n",
      "0.7463949334883446\n"
     ]
    }
   ],
   "source": [
    "import pandas\n",
    "import plotly.graph_objs as go\n",
    "import plotly.offline as py\n",
    "from collections import Counter\n",
    "from combo.models.classifier_stacking import Stacking\n",
    "from combo.models.classifier_comb import SimpleClassifierAggregator\n",
    "from imblearn.over_sampling import BorderlineSMOTE\n",
    "from scipy import stats\n",
    "from sklearn.metrics import auc, roc_curve\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier\n",
    "from sklearn.neural_network import MLPClassifier\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "\n",
    "\n",
    "factors = accounting_factors + market_factors + academic_factors\n",
    "\n",
    "\n",
    "# Stage 1\n",
    "stage1_models = {}\n",
    "stage1_auc = {}\n",
    "for i in \"abcde\":\n",
    "    predicted = f\"preFD_{i}\"\n",
    "\n",
    "    dataset = pandas.read_csv(\"../data/data.csv\", sep=\"\\t\")\n",
    "    # dataset = dataset[dataset['accper'] > \"2017-01-01\"]\n",
    "\n",
    "    X = stats.zscore(dataset[factors])\n",
    "    y = dataset[predicted]\n",
    "    X_train, X_test, y_train, y_test = train_test_split(\n",
    "        X, y, test_size=0.3, random_state=1\n",
    "    )\n",
    "\n",
    "    oversample = BorderlineSMOTE()\n",
    "    print(Counter(y_train))\n",
    "    X_train, y_train = oversample.fit_resample(X_train, y_train)\n",
    "    print(Counter(y_train))\n",
    "\n",
    "    classifiers = [\n",
    "        DecisionTreeClassifier(),\n",
    "        LogisticRegression(penalty=\"none\", solver=\"newton-cg\"),\n",
    "        MLPClassifier(hidden_layer_sizes=(100, 50, 10), max_iter=100000),\n",
    "        SVC(probability=True),\n",
    "        RandomForestClassifier(),\n",
    "        GradientBoostingClassifier(),\n",
    "    ]\n",
    "\n",
    "    stage1_models[i] = SimpleClassifierAggregator(\n",
    "        base_estimators=classifiers, method=\"average\"\n",
    "    )\n",
    "    stage1_models[i].fit(X_train, y_train)\n",
    "    prob = stage1_models[i].predict_proba(X_test)\n",
    "    fpr, tpr, thresholds = roc_curve(y_test, prob[:, 1], pos_label=1)\n",
    "    cur_auc = auc(fpr, tpr)\n",
    "    stage1_auc[i] = cur_auc\n",
    "    print(cur_auc)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Counter({False: 11547, True: 3190})\n",
      "14737\n",
      "Counter({False: 11547, True: 11547})\n",
      "23094\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\lc_zd\\AppData\\Local\\Temp\\ipykernel_10224\\82329927.py:23: FutureWarning:\n",
      "\n",
      "The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
      "\n",
      "C:\\Users\\lc_zd\\AppData\\Local\\Temp\\ipykernel_10224\\82329927.py:24: FutureWarning:\n",
      "\n",
      "The series.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Counter({True: 19904, False: 11547})\n",
      "Counter({False: 19904, True: 19904})\n"
     ]
    }
   ],
   "source": [
    "# Stage 2\n",
    "X = stats.zscore(dataset[factors])\n",
    "y = (\n",
    "    dataset[\"preFD_a\"]\n",
    "    + dataset[\"preFD_b\"]\n",
    "    + dataset[\"preFD_c\"]\n",
    "    + dataset[\"preFD_d\"]\n",
    "    + dataset[\"preFD_e\"]\n",
    ")\n",
    "y = y > 0\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1)\n",
    "\n",
    "oversample = BorderlineSMOTE()\n",
    "print(Counter(y_train))\n",
    "before = len(y_train)\n",
    "print(before)\n",
    "X_train, y_train = oversample.fit_resample(X_train, y_train)\n",
    "print(Counter(y_train))\n",
    "after = len(y_train)\n",
    "print(after)\n",
    "\n",
    "column_added = after - before\n",
    "X_train = X_train.append(X_train[before:after])\n",
    "y_train = y_train.append(y_train[before:after])\n",
    "print(Counter(y_train))\n",
    "X_train, y_train = oversample.fit_resample(X_train, y_train)\n",
    "print(Counter(y_train))\n",
    "\n",
    "X_train = X_train[after:]\n",
    "y_train = y_train[after:]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0.6411578676281227,\n",
       " 'b': 0.6348984952724329,\n",
       " 'c': 0.664377028071079,\n",
       " 'd': 0.6503341304587764,\n",
       " 'e': 0.6625398624038851}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "weight = {}\n",
    "for i in \"abcde\":\n",
    "    predicted_prob = stage1_models[i].predict_proba(X_test)\n",
    "    fpr, tpr, thresholds = roc_curve(y_test, predicted_prob[:, 1], pos_label=1)\n",
    "    cur_auc = auc(fpr, tpr)\n",
    "    weight[i] = cur_auc\n",
    "weight\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'final_model' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32mc:\\Users\\lc_zd\\dev\\distress-prediction\\notebooks\\model_combo.ipynb Cell 5'\u001b[0m in \u001b[0;36m<cell line: 4>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      <a href='vscode-notebook-cell:/c%3A/Users/lc_zd/dev/distress-prediction/notebooks/model_combo.ipynb#ch0000004?line=0'>1</a>\u001b[0m \u001b[39mfor\u001b[39;00m i \u001b[39min\u001b[39;00m \u001b[39m'\u001b[39m\u001b[39mabcde\u001b[39m\u001b[39m'\u001b[39m:\n\u001b[0;32m      <a href='vscode-notebook-cell:/c%3A/Users/lc_zd/dev/distress-prediction/notebooks/model_combo.ipynb#ch0000004?line=1'>2</a>\u001b[0m     stage1_models[i]\u001b[39m.\u001b[39mpredict_proba(X)\n\u001b[1;32m----> <a href='vscode-notebook-cell:/c%3A/Users/lc_zd/dev/distress-prediction/notebooks/model_combo.ipynb#ch0000004?line=3'>4</a>\u001b[0m final_prob \u001b[39m=\u001b[39m final_model\u001b[39m.\u001b[39mpredict_proba(X)\n\u001b[0;32m      <a href='vscode-notebook-cell:/c%3A/Users/lc_zd/dev/distress-prediction/notebooks/model_combo.ipynb#ch0000004?line=4'>5</a>\u001b[0m fpr, tpr, thresholds \u001b[39m=\u001b[39m roc_curve(y, final_prob[:, \u001b[39m1\u001b[39m], pos_label\u001b[39m=\u001b[39m\u001b[39m1\u001b[39m)\n\u001b[0;32m      <a href='vscode-notebook-cell:/c%3A/Users/lc_zd/dev/distress-prediction/notebooks/model_combo.ipynb#ch0000004?line=5'>6</a>\u001b[0m cur_auc \u001b[39m=\u001b[39m auc(fpr, tpr)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'final_model' is not defined"
     ]
    }
   ],
   "source": [
    "for i in 'abcde':\n",
    "    stage1_models[i].predict_proba(X)\n",
    "\n",
    "final_prob = final_model.predict_proba(X)\n",
    "fpr, tpr, thresholds = roc_curve(y, final_prob[:, 1], pos_label=1)\n",
    "cur_auc = auc(fpr, tpr)\n",
    "cur_auc\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "prob_a = stage1_models['a'].predict_proba(X)\n",
    "prob_b = stage1_models['b'].predict_proba(X)\n",
    "prob_c = stage1_models['c'].predict_proba(X)\n",
    "prob_d = stage1_models['d'].predict_proba(X)\n",
    "prob_e = stage1_models['e'].predict_proba(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "final_prob = prob_a[:, 1] * weight['a'] + prob_b[:, 1] * weight['b'] + prob_c[:, 1] * weight['c'] + prob_d[:, 1] * weight['d'] + prob_e[:, 1] * weight['e']\n",
    "final_prob = final_prob / (weight['a'] + weight['b'] + weight['c'] + weight['d'] + weight['e'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9191451408709062"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fpr, tpr, thresholds = roc_curve(y, final_prob, pos_label=1)\n",
    "cur_auc = auc(fpr, tpr)\n",
    "cur_auc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21054"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(final_prob)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Size</th>\n",
       "      <th>Growth</th>\n",
       "      <th>Lev</th>\n",
       "      <th>FCF</th>\n",
       "      <th>PPE</th>\n",
       "      <th>ROA</th>\n",
       "      <th>ROE</th>\n",
       "      <th>TobinQ</th>\n",
       "      <th>Top1</th>\n",
       "      <th>Top10</th>\n",
       "      <th>...</th>\n",
       "      <th>KZologit</th>\n",
       "      <th>SA</th>\n",
       "      <th>DA</th>\n",
       "      <th>REM</th>\n",
       "      <th>PLDdummy</th>\n",
       "      <th>ETR1</th>\n",
       "      <th>BTD</th>\n",
       "      <th>RiskT</th>\n",
       "      <th>avgLawTax</th>\n",
       "      <th>RPTratio</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2.270917</td>\n",
       "      <td>1.455071</td>\n",
       "      <td>0.795352</td>\n",
       "      <td>-1.110265</td>\n",
       "      <td>-1.357218</td>\n",
       "      <td>0.692677</td>\n",
       "      <td>0.927576</td>\n",
       "      <td>0.521145</td>\n",
       "      <td>-1.292886</td>\n",
       "      <td>-2.108430</td>\n",
       "      <td>...</td>\n",
       "      <td>1.419678</td>\n",
       "      <td>2.358068</td>\n",
       "      <td>3.298452</td>\n",
       "      <td>2.356570</td>\n",
       "      <td>-0.900523</td>\n",
       "      <td>1.262707</td>\n",
       "      <td>-1.092065</td>\n",
       "      <td>-0.101825</td>\n",
       "      <td>-0.656526</td>\n",
       "      <td>-0.881539</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2.404578</td>\n",
       "      <td>-0.061834</td>\n",
       "      <td>0.867972</td>\n",
       "      <td>-0.042035</td>\n",
       "      <td>-1.329949</td>\n",
       "      <td>0.246345</td>\n",
       "      <td>0.470607</td>\n",
       "      <td>-0.580090</td>\n",
       "      <td>-1.286136</td>\n",
       "      <td>-2.154444</td>\n",
       "      <td>...</td>\n",
       "      <td>0.432597</td>\n",
       "      <td>2.495940</td>\n",
       "      <td>0.426087</td>\n",
       "      <td>-1.088647</td>\n",
       "      <td>-0.900523</td>\n",
       "      <td>0.888374</td>\n",
       "      <td>0.475838</td>\n",
       "      <td>-0.228641</td>\n",
       "      <td>0.358458</td>\n",
       "      <td>-0.882590</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.514028</td>\n",
       "      <td>0.008197</td>\n",
       "      <td>0.843845</td>\n",
       "      <td>0.517564</td>\n",
       "      <td>-1.334198</td>\n",
       "      <td>0.364724</td>\n",
       "      <td>0.611797</td>\n",
       "      <td>-0.353089</td>\n",
       "      <td>-1.286136</td>\n",
       "      <td>-2.095283</td>\n",
       "      <td>...</td>\n",
       "      <td>0.080332</td>\n",
       "      <td>2.605487</td>\n",
       "      <td>-0.515190</td>\n",
       "      <td>-2.975501</td>\n",
       "      <td>-0.900523</td>\n",
       "      <td>0.865222</td>\n",
       "      <td>-0.672812</td>\n",
       "      <td>-0.620068</td>\n",
       "      <td>-1.159893</td>\n",
       "      <td>-0.044435</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2.857101</td>\n",
       "      <td>-0.272761</td>\n",
       "      <td>1.262921</td>\n",
       "      <td>0.183958</td>\n",
       "      <td>-1.357728</td>\n",
       "      <td>0.364404</td>\n",
       "      <td>0.747436</td>\n",
       "      <td>-0.652654</td>\n",
       "      <td>-1.286136</td>\n",
       "      <td>-2.104486</td>\n",
       "      <td>...</td>\n",
       "      <td>0.235395</td>\n",
       "      <td>3.011414</td>\n",
       "      <td>0.327096</td>\n",
       "      <td>0.139458</td>\n",
       "      <td>-0.900523</td>\n",
       "      <td>0.950014</td>\n",
       "      <td>0.010709</td>\n",
       "      <td>-0.648201</td>\n",
       "      <td>-0.087145</td>\n",
       "      <td>-0.856662</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2.998240</td>\n",
       "      <td>0.412488</td>\n",
       "      <td>1.394548</td>\n",
       "      <td>0.098131</td>\n",
       "      <td>-1.359230</td>\n",
       "      <td>0.292354</td>\n",
       "      <td>0.818886</td>\n",
       "      <td>-0.758967</td>\n",
       "      <td>-1.286136</td>\n",
       "      <td>-2.079506</td>\n",
       "      <td>...</td>\n",
       "      <td>0.616253</td>\n",
       "      <td>3.297490</td>\n",
       "      <td>0.038038</td>\n",
       "      <td>-0.755393</td>\n",
       "      <td>-0.900523</td>\n",
       "      <td>1.015282</td>\n",
       "      <td>0.174717</td>\n",
       "      <td>-0.745750</td>\n",
       "      <td>0.853572</td>\n",
       "      <td>-0.049603</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21049</th>\n",
       "      <td>-0.647768</td>\n",
       "      <td>0.199356</td>\n",
       "      <td>-1.736263</td>\n",
       "      <td>0.749707</td>\n",
       "      <td>-0.266733</td>\n",
       "      <td>1.809773</td>\n",
       "      <td>0.769605</td>\n",
       "      <td>-0.223028</td>\n",
       "      <td>1.063470</td>\n",
       "      <td>1.539853</td>\n",
       "      <td>...</td>\n",
       "      <td>-3.117069</td>\n",
       "      <td>-0.430018</td>\n",
       "      <td>0.320092</td>\n",
       "      <td>-1.061366</td>\n",
       "      <td>1.110466</td>\n",
       "      <td>0.296592</td>\n",
       "      <td>-0.101055</td>\n",
       "      <td>-0.780617</td>\n",
       "      <td>-0.928839</td>\n",
       "      <td>-0.710542</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21050</th>\n",
       "      <td>0.941806</td>\n",
       "      <td>6.786213</td>\n",
       "      <td>1.116189</td>\n",
       "      <td>0.155098</td>\n",
       "      <td>-0.316753</td>\n",
       "      <td>0.149903</td>\n",
       "      <td>0.348679</td>\n",
       "      <td>-0.693820</td>\n",
       "      <td>-0.088046</td>\n",
       "      <td>2.026291</td>\n",
       "      <td>...</td>\n",
       "      <td>-3.221807</td>\n",
       "      <td>1.134521</td>\n",
       "      <td>-0.708556</td>\n",
       "      <td>-2.905012</td>\n",
       "      <td>1.110466</td>\n",
       "      <td>0.784916</td>\n",
       "      <td>-2.188354</td>\n",
       "      <td>0.333304</td>\n",
       "      <td>-0.928839</td>\n",
       "      <td>-0.164218</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21051</th>\n",
       "      <td>0.931312</td>\n",
       "      <td>-0.569224</td>\n",
       "      <td>1.205861</td>\n",
       "      <td>0.201219</td>\n",
       "      <td>-0.156741</td>\n",
       "      <td>-0.696813</td>\n",
       "      <td>-0.650554</td>\n",
       "      <td>-0.676195</td>\n",
       "      <td>-0.083321</td>\n",
       "      <td>2.027605</td>\n",
       "      <td>...</td>\n",
       "      <td>0.724779</td>\n",
       "      <td>1.097152</td>\n",
       "      <td>-0.248626</td>\n",
       "      <td>0.269455</td>\n",
       "      <td>1.110466</td>\n",
       "      <td>0.572108</td>\n",
       "      <td>0.580933</td>\n",
       "      <td>1.113621</td>\n",
       "      <td>-0.928839</td>\n",
       "      <td>-0.529327</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21052</th>\n",
       "      <td>-0.719654</td>\n",
       "      <td>-0.267128</td>\n",
       "      <td>-0.643661</td>\n",
       "      <td>-0.305274</td>\n",
       "      <td>0.027116</td>\n",
       "      <td>0.263309</td>\n",
       "      <td>0.136799</td>\n",
       "      <td>0.225645</td>\n",
       "      <td>0.143472</td>\n",
       "      <td>-0.224470</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.400909</td>\n",
       "      <td>-0.555257</td>\n",
       "      <td>-0.108312</td>\n",
       "      <td>-1.296914</td>\n",
       "      <td>1.110466</td>\n",
       "      <td>0.254945</td>\n",
       "      <td>-0.522581</td>\n",
       "      <td>-0.559950</td>\n",
       "      <td>-0.928839</td>\n",
       "      <td>-0.480668</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21053</th>\n",
       "      <td>-0.630216</td>\n",
       "      <td>-0.034059</td>\n",
       "      <td>-0.434331</td>\n",
       "      <td>0.360780</td>\n",
       "      <td>-0.052044</td>\n",
       "      <td>-0.011908</td>\n",
       "      <td>-0.001793</td>\n",
       "      <td>-0.291226</td>\n",
       "      <td>0.171821</td>\n",
       "      <td>-0.187658</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.983620</td>\n",
       "      <td>-0.497228</td>\n",
       "      <td>-0.558213</td>\n",
       "      <td>-0.876354</td>\n",
       "      <td>1.110466</td>\n",
       "      <td>0.454746</td>\n",
       "      <td>-0.087069</td>\n",
       "      <td>-0.591412</td>\n",
       "      <td>-0.928839</td>\n",
       "      <td>-0.776211</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>21054 rows × 45 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           Size    Growth       Lev       FCF       PPE       ROA       ROE  \\\n",
       "0      2.270917  1.455071  0.795352 -1.110265 -1.357218  0.692677  0.927576   \n",
       "1      2.404578 -0.061834  0.867972 -0.042035 -1.329949  0.246345  0.470607   \n",
       "2      2.514028  0.008197  0.843845  0.517564 -1.334198  0.364724  0.611797   \n",
       "3      2.857101 -0.272761  1.262921  0.183958 -1.357728  0.364404  0.747436   \n",
       "4      2.998240  0.412488  1.394548  0.098131 -1.359230  0.292354  0.818886   \n",
       "...         ...       ...       ...       ...       ...       ...       ...   \n",
       "21049 -0.647768  0.199356 -1.736263  0.749707 -0.266733  1.809773  0.769605   \n",
       "21050  0.941806  6.786213  1.116189  0.155098 -0.316753  0.149903  0.348679   \n",
       "21051  0.931312 -0.569224  1.205861  0.201219 -0.156741 -0.696813 -0.650554   \n",
       "21052 -0.719654 -0.267128 -0.643661 -0.305274  0.027116  0.263309  0.136799   \n",
       "21053 -0.630216 -0.034059 -0.434331  0.360780 -0.052044 -0.011908 -0.001793   \n",
       "\n",
       "         TobinQ      Top1     Top10  ...  KZologit        SA        DA  \\\n",
       "0      0.521145 -1.292886 -2.108430  ...  1.419678  2.358068  3.298452   \n",
       "1     -0.580090 -1.286136 -2.154444  ...  0.432597  2.495940  0.426087   \n",
       "2     -0.353089 -1.286136 -2.095283  ...  0.080332  2.605487 -0.515190   \n",
       "3     -0.652654 -1.286136 -2.104486  ...  0.235395  3.011414  0.327096   \n",
       "4     -0.758967 -1.286136 -2.079506  ...  0.616253  3.297490  0.038038   \n",
       "...         ...       ...       ...  ...       ...       ...       ...   \n",
       "21049 -0.223028  1.063470  1.539853  ... -3.117069 -0.430018  0.320092   \n",
       "21050 -0.693820 -0.088046  2.026291  ... -3.221807  1.134521 -0.708556   \n",
       "21051 -0.676195 -0.083321  2.027605  ...  0.724779  1.097152 -0.248626   \n",
       "21052  0.225645  0.143472 -0.224470  ... -0.400909 -0.555257 -0.108312   \n",
       "21053 -0.291226  0.171821 -0.187658  ... -0.983620 -0.497228 -0.558213   \n",
       "\n",
       "            REM  PLDdummy      ETR1       BTD     RiskT  avgLawTax  RPTratio  \n",
       "0      2.356570 -0.900523  1.262707 -1.092065 -0.101825  -0.656526 -0.881539  \n",
       "1     -1.088647 -0.900523  0.888374  0.475838 -0.228641   0.358458 -0.882590  \n",
       "2     -2.975501 -0.900523  0.865222 -0.672812 -0.620068  -1.159893 -0.044435  \n",
       "3      0.139458 -0.900523  0.950014  0.010709 -0.648201  -0.087145 -0.856662  \n",
       "4     -0.755393 -0.900523  1.015282  0.174717 -0.745750   0.853572 -0.049603  \n",
       "...         ...       ...       ...       ...       ...        ...       ...  \n",
       "21049 -1.061366  1.110466  0.296592 -0.101055 -0.780617  -0.928839 -0.710542  \n",
       "21050 -2.905012  1.110466  0.784916 -2.188354  0.333304  -0.928839 -0.164218  \n",
       "21051  0.269455  1.110466  0.572108  0.580933  1.113621  -0.928839 -0.529327  \n",
       "21052 -1.296914  1.110466  0.254945 -0.522581 -0.559950  -0.928839 -0.480668  \n",
       "21053 -0.876354  1.110466  0.454746 -0.087069 -0.591412  -0.928839 -0.776211  \n",
       "\n",
       "[21054 rows x 45 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\lc_zd\\AppData\\Local\\Temp\\ipykernel_10224\\2819193451.py:2: SettingWithCopyWarning:\n",
      "\n",
      "\n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "\n"
     ]
    }
   ],
   "source": [
    "fd_risk = dataset[['stkcd', 'accper']]\n",
    "fd_risk['fd_risk'] = final_prob"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "fd_risk.to_csv('../data/fd_risk.csv', sep=',', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "d5dc63dd1c9ddf0581ef2da2b10ea022e45c2dc66e7a651ff49739c463f2c578"
  },
  "kernelspec": {
   "display_name": "Python 3.8.13 ('fdpredict')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
